package tech.abing.spider.task.crawler.amac;

import java.util.Arrays;
import java.util.List;

import javax.management.JMException;

import tech.abing.spider.CrawTaskBuilder;
import tech.abing.spider.CrawlManager;
import tech.abing.spider.CrawlTask;
import tech.abing.spider.component.DynamicEntrance;
import tech.abing.spider.component.listener.TaskLifeListener;
import tech.abing.spider.downloader.impl.DefaultPageDownloader;
import tech.abing.spider.monitor.SpiderMonitor;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.DictEnum;
import tech.abing.spider.task.pageprocessor.amac.LostPrivPlaceOrgProcessor;
import tech.abing.spider.task.pipeline.amac.LostPrivPlaceOrgPipeline;
import tech.abing.spider.util.SqlUtil;

/**
 * 抓取失联私募机构名单页面
 * 
 * @author ZZW
 *
 */
public class LostPrivPlaceOrgSpider {

	/**
	 * @param args
	 */
	private static SqlUtil sqlUtil = null;

	public static void start(int thread, int politenessDelay) {

		CrawlManager manager = CrawlManager.getInstance();

		CrawTaskBuilder builder = manager.prepareCrawlTask(DictEnum.TASK_TYPE_LOST_PRIV_PLACE_ORG_HTML.getName(), DefaultPageDownloader.class);
		builder.useThread(thread);
		builder.useDynamicEntrance(DynamicEntranceImpl.class);
		builder.usePageEncoding(PageEncoding.UTF8);
		builder.usePageRetryCount(3);
		builder.usePipeline(LostPrivPlaceOrgPipeline.class).setPolitenessDelay(politenessDelay);
		builder.useTaskLifeListener(new TaskLifeListener() {

			@Override
			public void onStart(CrawlTask task) {
				if (sqlUtil == null) {
					sqlUtil = SqlUtil.getInstance();
				}
				sqlUtil.deleteForConn("DELETE FROM " + DictEnum.TASK_TYPE_LOST_PRIV_PLACE_ORG_HTML.getCode());
				System.out.println("================开始");
			}

			@Override
			public void onFinished(CrawlTask task) {
				task.getPipeline().destory();
				sqlUtil.closeConn();
				sqlUtil = null;
				System.out.println("================完成");
			}

		});

		CrawlTask task = builder.build();

		try {
			SpiderMonitor.instance().register(task);
		} catch (JMException e) {
			e.printStackTrace();
		}

		manager.start(task);
	}

	public static void start() {
		start(20, 200);
	}

	public static final class DynamicEntranceImpl extends DynamicEntrance {

		@Override
		public List<StartContext> loadStartContext() {

			StartContext context = new StartContext();

			PageRequest req = buildReq(context);
			context.injectSeed(req);

			return Arrays.asList(context);
		}

		private PageRequest buildReq(StartContext context) {
			String url = "http://www.amac.org.cn/xhdt/zxdt/index.shtml";

			PageRequest req = context.createPageRequest(url, LostPrivPlaceOrgProcessor.class);
			req.setMethod(Method.GET);
			req.setReqPayloadParams("{}");
			req.putHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
			req.putHeader("Accept-Encoding", "gzip, deflate, sdch");
			req.putHeader("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");
			req.putHeader("Connection", "keep-alive");
			req.putHeader("Host", "www.amac.org.cn");
			req.putHeader("Referer", "http://www.amac.org.cn/cms/browsepriv/catalog?ID=11253");
			req.putHeader("Upgrade-Insecure-Requests:", "1");
			req.putHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
			return req;
		}

	}

}